<?php
/**
 * Fastlib核心驱动
 * 
 * $Id: core.php 130 2010-10-10 16:47:37Z Administrator $
 * $Revision: 130 $
 * $Author: Administrator $
 * $Date: 2010-10-10 16:47:37 +0000 (Sun, 10 Oct 2010) $
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage functions
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 安全创建新目录
 *
 * @param string $dir 目录名称
 * @param int $chmod 类Linux下的文件夹权限
 */
function touchdir($dir, $chmod = 0744) {
	if (! is_dir ( $dir )) {
		mkdir ( $dir, $chmod );
		touch ( $dir . '/index.html' );
		touch ( $dir . '/index.php' );
	}
}
/**
 * 返回站点根目录
 * 
 * 在同一空间内建立多个域名站点时，本函数可以直接返回
 * 站点根目录。返回完整目录一般用于文件操作。而短目录
 * 可以用于站点上的文件路径。比较说图片路径.
 *
 * @param bool $fullpath 返回完整路径
 * @return string
 */
function getSiteCurrent($fullpath = false) {
	$path = '';
	if ($fullpath) {
		if (defined ( '__SITE__' )) {
			$path .= __SITE__;
		}
	}
	if (defined ( '__SITECURRENT__' )) {
		$path .= __SITECURRENT__ . '/';
	}
	return $path;
}
/**
 * 变量彩色输出
 * 
 * 只要调试时有效
 *
 * @param mixed $var 变量
 * @param string $color 色彩(HTML)
 * @param bool $output 直接输出
 * @return string 输出
 */
function printColorVar($var, $color = 'php', $output = true) {
	if (__DEBUG__) {
		if (strtolower ( $color ) == 'php') {
			if ($output) {
				echo str_replace ( "&lt;?", '', highlight_string ( '<?' . var_export ( $var, true ), true ) );
			} else {
				return str_replace ( "&lt;?", '', highlight_string ( '<?' . var_export ( $var, true ), true ) );
			}
		} else {
			if ($output) {
				echo '<font color=', $color, '>', var_export ( $var, true ), '</font>';
			} else {
				return '<font color=' . $color . '>' . var_export ( $var, true ) . '</font>';
			}
		}
	}
	return '';
}
/**
 * 返回指定长度的随机字符
 *
 * @param int $length 反要求的字符串长度
 * @param bool $read 是否易于阅读
 * @return string 字符串
 */
function randString($length = 5, $readable = true) {
	if ($readable) {
		$stringTable = 'BCEFGHJKMPQRTVWXY2346789';
	} else {
		$stringTable = 'ABCDEFGHIJKLMNOPQRSTUVWXY0123456789abcdefghijklmnopqrstuvwxyz+-*/';
	}
	$return = '';
	for(; $length; $length --) {
		$return .= $stringTable {rand ( 0, strlen ( $stringTable ) - 1 )};
	}
	return $return;
}
/**
 * FASTLIB信息
 *
 */
function fastinfo($args = null) {
	if (is_string ( $args )) {
		$__IMAGE = null;
		require __FASTSYSTEM__ . 'driver/etc/image.php';
		foreach ( $__IMAGE as $k => $v ) {
			if (strtolower ( substr ( md5 ( $k . date ( "Y-m-d" ) ), 8, 16 ) ) === $args) {
				reset ( $v );
				list ( , $value ) = each ( $v );
				header ( "content-type: image/" . $value );
				while ( false != (list ( , $value ) = each ( $v )) ) {
					echo chr ( $value );
				}
				exit ( 0 );
			}
		}
	}
	if (! function_exists ( 'print_html' )) {
		function print_html($title, $body) {
			echo <<<ET
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}
.center table { margin-left: auto; margin-right: auto; text-align: left;}
.center th { text-align: center !important; }
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.h {background-color: #9999cc; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
.vr {background-color: #cccccc; text-align: right; color: #000000;}
img.right {float: right; border: 0px;}
hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
</style>
<title>$title</title>
<meta name="author" content="glzaboy,codingspeed.com" />
<meta name="keyword" content="fastlib,php" />
<meta name="description" content="fastlib info page." />
</head>
<body><div class="center">$body</div></body></html>
ET;
		}
		function printHeader() {
			return "<table width=\"600\">";
		}
		function startTable() {
			return "<table width=\"600\">";
		}
		function endTable() {
			return "</table><br />" . PHP_EOL;
		}
		function printRow($rowArr) {
			$rowbody = '';
			if (is_array ( $rowArr )) {
				foreach ( $rowArr as $cell ) {
					$cellattr = '';
					if (@$cell ['class']) {
						$cellattr .= ' class ="' . $cell ['class'] . '"';
					}
					if (@$cell ['colspan']) {
						$cellattr .= ' colspan="' . $cell ['colspan'] . '"';
					}
					if (@$cell ['rowspan']) {
						$cellattr .= ' rowspan="' . $cell ['rowspan'] . '"';
					}
					if (@$cell ['pre']) {
						$rowbody .= '<td' . $cellattr . '><pre>' . strip_tags ( @$cell ['content'] ? $cell ['content'] : $cell [0], '<a>,<center>,<pre>,<u>,<i>,<b>,<br>,<img>' ) . '</pre></td>';
					} else {
						$rowbody .= '<td' . $cellattr . '>' . strip_tags ( @$cell ['content'] ? $cell ['content'] : $cell [0], '<a>,<center>,<pre>,<u>,<i>,<b>,<br>,<img>' ) . '</td>';
					}
				}
			}
			return '<tr>' . $rowbody . '</tr>' . PHP_EOL;
		}
	}
	$body = '';
	$body .= startTable ();
	$body .= printRow ( array (array ('class' => 'h', 'content' => '<img border="0" class="right" src="?' . substr ( md5 ( 'fast' . date ( 'Y-m-d' ) ), 8, 16 ) . substr ( md5 ( 'logo' . date ( 'Y-m-d' ) ), 8, 16 ) . '" alt="Fastlib Logo" /><h1>FASTLIB</h1>' ) ) );
	$body .= endTable ();
	$body .= startTable ();
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'FASTLIB PATH' ), array ('class' => 'v', 'content' => __FASTLIB__ ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'Fastlib Version' ), array ('class' => 'v', 'content' => __FASTVER__ ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'Help and  New version' ), array ('class' => 'v', 'content' => '<a href="http://www.codingspeed.com/soft.php?fastlib:' . __FASTVER__ . '">www.codingspeed.com</a>' ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'Server SoftWare' ), array ('class' => 'v', 'content' => $_SERVER ['SERVER_SOFTWARE'] ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'PHP Version' ), array ('class' => 'v', 'content' => PHP_VERSION ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'GATEWAY_INTERFACE' ), array ('class' => 'v', 'content' => $_SERVER ['GATEWAY_INTERFACE'] ) ) );
	$body .= printRow ( array (array ('class' => 'e', 'content' => 'SERVER_PROTOCOL' ), array ('class' => 'v', 'content' => $_SERVER ['SERVER_PROTOCOL'] ) ) );
	$body .= endTable ();
	$dir = dir ( __FASTSYSTEM__ . 'class' );
	for($r = $dir->read (); $r;) {
		if (is_file ( __FASTSYSTEM__ . 'class/' . $r )) {
			if (! class_exists ( substr ( $r, 0, - 4 ) ) && strtolower ( substr ( $r, - 3 ) ) == 'php') {
				FastLoad::fastinclude ( 'class/' . chop ( $r ), 'require' );
			}
			if (method_exists ( substr ( $r, 0, - 4 ), 'printInfo' )) {
				$body .= call_user_func ( array (substr ( $r, 0, - 4 ), 'printInfo' ) );
			} else {
				$body .= startTable ();
				$body .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . substr ( $r, 0, - 4 ) . '</center>' ) ) );
				$body .= printRow ( array (array ('class' => 'e', 'content' => 'the file info not exists.' ) ) );
				$body .= endTable ();
			}
		}
		$r = $dir->read ();
	}
	$dir = dir ( __FASTSYSTEM__ . 'functions' );
	for($r = $dir->read (); $r;) {
		if (is_file ( __FASTSYSTEM__ . 'functions/' . $r ) && $r !== 'core.php' && strtolower ( substr ( $r, - 3 ) ) == 'php') {
			FastLoad::fastinclude ( 'functions/' . chop ( $r ), 'require_once' );
			if (function_exists ( substr ( $r, 0, - 14 ) . 'printInfo' )) {
				$body .= @call_user_func ( substr ( $r, 0, - 14 ) . 'printInfo' );
			} else {
				$body .= startTable ();
				$body .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . substr ( $r, 0, - 4 ) . '</center>' ) ) );
				$body .= printRow ( array (array ('class' => 'e', 'content' => 'the file info not exists.' ) ) );
				$body .= endTable ();
			}
		}
		$r = $dir->read ();
	}
	$body .= startTable ();
	$body .= printRow ( array (array ('class' => 'v', 'content' => 'This software powered by glzaboy.If you found some error please mail us ,our mail <a href="mailto:iceer.coffee@gmail.com">iceer.coffee@gmail.com</a><br /> &copy; 2006-' . date ( 'Y' ) . '<br /><img src="?' . substr ( md5 ( 'fast' . date ( 'Y-m-d' ) ), 8, 16 ) . substr ( md5 ( 'author' . date ( 'Y-m-d' ) ), 8, 16 ) . '" alt="fastlib Logo,codingspeed.com" /><img src="?=' . php_logo_guid () . '" alt="PHP.net" /><img src="?=' . zend_logo_guid () . '" alt="zend.com" />' ) ) );
	$body .= endTable ();
	print_html ( 'fastinfo()', $body );
}